package com.gdut.simple.presenter.user;

import com.gdut.simple.common.RequestCallback;
import com.gdut.simple.contract.CommentContract;
import com.gdut.simple.data.entity.Comment;
import com.gdut.simple.model.ICommentModel;
import com.gdut.simple.model.impl.CommentModelImpl;

import java.util.List;

public class CommentPresenter implements CommentContract.Presenter {

    private CommentContract.View mCommentView;
    private ICommentModel mCommentModel;

    public CommentPresenter(CommentContract.View mCommentView) {
        this.mCommentView = mCommentView;
        this.mCommentModel = new CommentModelImpl();
        this.mCommentView.setPresenter(this);
    }

    @Override
    public void refresh() {
        if (mCommentView.getUser() == null) {
            return;
        }
        mCommentView.showRefreshProgress();
        mCommentModel.refresh(mCommentView.getUser(), new RequestCallback<List<Comment>>() {
            @Override
            public void onSuccess(List<Comment> comments) {
                mCommentView.hideRefreshProgress();
                mCommentView.getItemList().clear();
                mCommentView.getItemList().addAll(comments);
                mCommentView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mCommentView.hideRefreshProgress();
                mCommentView.showMessage(message);
            }
        });
    }

    @Override
    public void loadMore(int page) {
        mCommentModel.loadMore(mCommentView.getUser(), page, new RequestCallback<List<Comment>>() {
            @Override
            public void onSuccess(List<Comment> comments) {
                mCommentView.getItemList().addAll(comments);
                mCommentView.refreshList();
            }

            @Override
            public void onFailure(String message) {
                mCommentView.showMessage(message);
            }
        });
    }
}
